from configparser import ConfigParser
import json

cfg = ConfigParser()
cfg.read('mysql.ini')

d = {}

with open('mysql.json', 'w') as f:

# 方法1：
#    for section in cfg.sections():
#        d[section] = dict(cfg.items(section))


# 方法2：字典生成式,sections()没有显示DEFAULT块
    #d = {section : {k:v for k, v in cfg.items(section)} for section in cfg.sections()}



# 方法3：字典生成式,items()显示DEFAULT块
    d = {section : {k:v for k, v in cfg.items(section)} for section, _ in cfg.items()}
        
    json.dump(d, f)
    

